package com.ruoyi.common;

import com.alibaba.fastjson2.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
/**
 * 该类定义get，post，put，patch等请求
 * 用于与kong进行交互
 */
public class Requestcommon {
    private RestTemplate restTemplate = new RestTemplate();
    private String response = "";
    public String PostKong(String path, JSONObject requestBody) {
        if (requestBody != null) {
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            HttpEntity entity = new HttpEntity<>(requestBody, headers);
            // 修改请求的URL，添加错误处理机制
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(path, entity, String.class);
            // 检查响应状态码
            response = responseEntity.getBody().toString();
            JSONObject re= JSONObject.parseObject(response);
            re.put("status",responseEntity.getStatusCodeValue() );
            response=re.toJSONString();
            if ((responseEntity.getStatusCodeValue() == 200)||(responseEntity.getStatusCodeValue() == 201)) {
                System.out.println(path+":ok");
            } else {
                // 处理错误情况
                System.out.println("Error: " + responseEntity.getStatusCodeValue() + " - " + responseEntity.getBody());
            }
        } else {
            response = "请求参数为空";
        }
        return response;
    }


    public String GetKong(String path) {

        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "application/json");
        HttpEntity entity = new HttpEntity<>(headers);

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(path, String.class);
        response = responseEntity.getBody().toString();
        JSONObject re= JSONObject.parseObject(response);
        re.put("status",responseEntity.getStatusCodeValue() );
        response=re.toJSONString();
        if ((responseEntity.getStatusCodeValue() == 200)||(responseEntity.getStatusCodeValue() == 201)) {

            System.out.println("ok");
        } else {
            // 处理错误情况
            System.out.println("Error: " + responseEntity.getStatusCodeValue() + " - " + responseEntity.getBody());
        }
        return response;
    }


    public String DeleteKong(String path) {
        if (path != null) {
            HttpHeaders headers = new HttpHeaders();
            headers.set("accept", "*/*");
            HttpEntity entity = new HttpEntity<>(headers);
            System.out.println(path);
            ResponseEntity<String> responseEntity = restTemplate.exchange(path, HttpMethod.DELETE, entity, String.class);
            if(responseEntity.getBody()==null){
                response="{}";
            }else {
                response = responseEntity.getBody().toString();
            }
            JSONObject re= JSONObject.parseObject(response);
            re.put("status",responseEntity.getStatusCodeValue() );
            response=re.toJSONString();
            if ((responseEntity.getStatusCodeValue() == 200)||(responseEntity.getStatusCodeValue() == 201)) {
                System.out.println("ok");
            } else {
                System.out.println("Error: " + responseEntity.getStatusCodeValue() + " - " + responseEntity.getBody());
            }
        } else {
            return "Error: path为空";
        }
        return response;

    }


    public String PutKong(String path, JSONObject requestBody) {
        if (requestBody != null) {
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type", "application/json");
            HttpEntity entity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(path, HttpMethod.PUT, entity, String.class);
            response = responseEntity.getBody().toString();
            JSONObject re= JSONObject.parseObject(response);
            re.put("status",responseEntity.getStatusCodeValue() );
            response=re.toJSONString();
            if ((responseEntity.getStatusCodeValue() == 200)||(responseEntity.getStatusCodeValue() == 201)) {
                System.out.println("ok");
            } else {
                // 处理错误情况
                System.out.println("Error: " + responseEntity.getStatusCodeValue() + " - " + responseEntity.getBody());
            }
        } else {
            return "Error:requestBody为空";
        }
        return response;
    }

    public String PatchKong(String path, JSONObject requestBody) {
        if (requestBody != null) {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPatch httpPatch = new HttpPatch(path);
            httpPatch.addHeader("Content-Type", "application/json");
            httpPatch.setEntity(new StringEntity(requestBody.toJSONString(), ContentType.APPLICATION_JSON));
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpPatch);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                this.response = EntityUtils.toString(response.getEntity());
                JSONObject re= JSONObject.parseObject(this.response);
                re.put("status", response.getStatusLine().getStatusCode());
                this.response=re.toJSONString();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (response.getStatusLine().getStatusCode() == 200) {

                System.out.println( this.response);
            } else {
                System.out.println("Error: " + response.getStatusLine().getStatusCode() + " - " + this.response);
            }
        } else {
            return "Error:requestBody为空";
        }
        return this.response;
    }

}
